#include"worker.h"

void *threadFunc(void * pargs)
{
    threadPool_t *pthreadPool=(threadPool_t *) pargs;
    while (1)
    {
        int netfd=-1;
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        if (pthreadPool->isexit==1)
        {
            puts("thread off!!");
            pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        while (pthreadPool->taskQueue.queueSize == 0)
        {
            puts("thread ready!!");
            pthread_cond_wait(&pthreadPool->taskQueue.cond, &pthreadPool->taskQueue.mutex);
            puts("thread do the job!!");
            if (pthreadPool->isexit)
            {
                puts("thread off!!");
                pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
                pthread_exit(NULL);
            }
        }
        netfd = DetaskQueue(&pthreadPool->taskQueue); // 取一个任务
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
        //业务代码
        assignJobs(netfd, pthreadPool);
        close(netfd);
        puts("thread mission accomplished!!");
        //业务代码
    }
}

int makeWorker(threadPool_t *pthreadPool)
{
    for (int i = 0; i < pthreadPool->threadNum; i++)
    {
        puts("thread create!!");
        pthread_t tid;
        pthread_create(&tid, NULL, threadFunc, pthreadPool);
        pthreadPool->tidArr[i] = tid;
    }
}